<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use UCloud\USMS\Apis\SendUSMSMessageRequest;
use UCloud\USMS\USMSClient;

/**
 * 短信接口
 */
class TextMessageCmsController {

    //发送通知短信
    public function sendMessage(Request $request) {
        $phone = $request->input('phone');
        $client = new USMSClient([
            "publicKey" => "4eZCGoHHvvz54rxKeinwG2DXu0jmYiS8R",
            "privateKey" => "6KZhaAGDpiK4suL9aSwpb48ozEgr4FSWH2kqBFIwbhkM",
            "projectId" => "org-4zlg4g",
        ]);

        $req = new SendUSMSMessageRequest();
        $req->setPhoneNumbers([$phone]);
        $req->setSigContent("SIG202501101FD2A3");
        $req->setTemplateId("UTN250117128N4L");
        $req->setTemplateParams([]);
        $client->sendUSMSMessage($req);
    }
    //

//    public function getCode(Request $request)
//    {
//        $phone = $request->input('phone');
//
//        $key = sprintf("sms_%s", $phone);
//        $lock_key = sprintf("sms_%s_lock", $phone);
//
//        if (Cache::has($lock_key)) {
//            return response()->json([
//                'msg' => "请不要频繁操作",
//                'code' => 500,
//                'data' => null,
//            ]);
//        }
//        $sms_code = random_int(100000, 999999);
//        $client = new USMSClient([
//            "publicKey" => "4eZCGoHHvvz54rxKeinwG2DXu0jmYiS8R",
//            "privateKey" => "6KZhaAGDpiK4suL9aSwpb48ozEgr4FSWH2kqBFIwbhkM",
//            "projectId" => "org-4zlg4g",
//        ]);
//
//        $req = new SendUSMSMessageRequest();
//        $req->setPhoneNumbers([$phone]);
//        $req->setSigContent("SIG202501101FD2A3");
//        $req->setTemplateId("UTA250421R1EIZK");
//        $req->setTemplateParams([$sms_code]);
//        $client->sendUSMSMessage($req);
//
//
//
//        Cache::put($key, $sms_code, 1800);
//        Cache::put($lock_key, true, 59);
//        return response()->json([
//            'msg' => '短信发送成功',
//            'code' => 0,
//            'data' => "短信发送成功",
//        ]);
//
//    }

    public function getCode(Request $request)
    {
        $phone = $request->input('phone');
        $sms_code = $request->input('sms_code');

        $client = new USMSClient([
            "publicKey" => "4eZCGoHHvvz54rxKeinwG2DXu0jmYiS8R",
            "privateKey" => "6KZhaAGDpiK4suL9aSwpb48ozEgr4FSWH2kqBFIwbhkM",
            "projectId" => "org-4zlg4g",
        ]);

        $req = new SendUSMSMessageRequest();
        $req->setPhoneNumbers([$phone]);
        $req->setSigContent("SIG202501101FD2A3");
        $req->setTemplateId("UTA250421R1EIZK");
        $req->setTemplateParams([$sms_code]);
        $client->sendUSMSMessage($req);

    }

}
